package com.company.project.controller;

import com.alibaba.fastjson.JSONObject;
import com.company.project.common.aop.annotation.LogAnnotation;
import com.company.project.common.exception.BusinessException;
import com.company.project.common.exception.code.BaseResponseCode;
import com.company.project.common.utils.DateUtils;
import com.company.project.common.utils.Sign;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;

import com.company.project.common.utils.DataResult;

import com.company.project.entity.SysFileEntity;
import com.company.project.service.SysFileService;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 文件管理
 *
 * @author YangYulin
 * @email YangYulinit@163.com
 * @date 2020-07-08 18:06:10
 */
@Controller
@RequestMapping("/")
@Api(tags = "文件管理")
@Slf4j
public class SysFileController {
    @Autowired
    private SysFileService sysFileService;

    /**
     * 跳转到页面
     */
    @GetMapping("/index/sysFile")
    public String sysFile() {
        return "sysfile/list";
    }

    @ApiOperation(value = "新增")
    @PostMapping("sysFile/add")
    @RequiresPermissions("sysFile:add")
    @ResponseBody
    public DataResult add(@RequestBody SysFileEntity sysFile) {
        sysFileService.save(sysFile);
        return DataResult.success();
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("sysFile/delete")
    @RequiresPermissions("sysFile:delete")
    @ResponseBody
    public DataResult delete(@RequestBody @ApiParam(value = "id集合") List<String> ids) {
        sysFileService.removeByIds(ids);
        return DataResult.success();
    }

    @ApiOperation(value = "更新")
    @PutMapping("sysFile/update")
    @RequiresPermissions("sysFile:update")
    @ResponseBody
    public DataResult update(@RequestBody SysFileEntity sysFile) {
        sysFileService.updateById(sysFile);
        return DataResult.success();
    }

    @ApiOperation(value = "查询分页数据")
    @PostMapping("sysFile/listByPage")
    @RequiresPermissions("sysFile:list")
    @ResponseBody

    public DataResult findListByPage(@RequestBody SysFileEntity sysFile) {
        Page page = new Page(sysFile.getPage(), sysFile.getLimit());
        QueryWrapper queryWrapper = new QueryWrapper();
        //查询条件示例
        //queryWrapper.eq("id", sysFile.getId());
        queryWrapper.orderByDesc("create_time");
        IPage<SysFileEntity> iPage = sysFileService.page(page, queryWrapper);
        return DataResult.success(iPage);
    }

    @ApiOperation(value = "查询图片信息")
    @PostMapping("sysFile/details")
    @ResponseBody
    public DataResult<List<SysFileEntity>> details(@RequestBody @ApiParam(value = "id集合") List<String> ids) {
        log.info(JSONObject.toJSONString(ids));
        return DataResult.success(sysFileService.listByIds(ids));
    }




    @ApiOperation(value = "文件上传")
    @PostMapping("sysFile/uploadImg")
    @Transactional(rollbackFor = Exception.class)
    @ResponseBody
    @LogAnnotation(title = "系统管理", action = "文件上传")
    public DataResult<SysFileEntity> uploadImg(@RequestParam("file") MultipartFile uploadFile, HttpServletRequest request) {
        log.info("文件上传");
        if (uploadFile.isEmpty()) {
            //返回选择文件提示
            return DataResult.getResult(BaseResponseCode.DATA_ERROR);
        }
        SysFileEntity sysFileEntity = null;
        try {
            sysFileEntity = sysFileService.uploadFile(uploadFile,request);
        } catch (IOException e) {
            throw new BusinessException("上传图片失败");
        }
        return DataResult.success(sysFileEntity);
    }

    @ApiOperation(value = "获取图片-以ImageIO流形式写回")
    @GetMapping(value = "sysFile/imageshow/{id}")
    @LogAnnotation(title = "系统管理", action = "获取图片流")
    public void getImg(@PathVariable("id") String id, HttpServletResponse response) throws IOException {
        log.info("-----------------------------------获取图片流-----------------------------------");
        sysFileService.resSterm(id,response);
    }
}
